#!/usr/bin/env python
# -*- coding: UTF-8 -*-
"""
@Project    :icac2 
@File       :queryset.py
@Author     :wangfan13
@Date       :2021/5/26 12:48 
"""
from django.db import models
from django.apps import apps


def get_child_queryset_u(check_queryset, obj, has_parent=True):
    """
    获取所有子集，查的范围checkQueryset，父级obj，是否包含默认True
    :param check_queryset:
    :param obj:
    :param has_parent:
    :return:
    """
    cls = type(obj)
    queryset = cls.objects.none()
    parent_queryset = cls.objects.filter(pk=obj.id)
    if has_parent:
        queryset = queryset | parent_queryset
    child_queryset = check_queryset.filter(parent=obj)
    while child_queryset:
        queryset = queryset | child_queryset
        child_queryset = check_queryset.filter(parent__in=child_queryset)
    return queryset


def get_child_queryset2(obj, has_parent=True):
    """获取所有子集，obj实例，数据表需包含parent字段，是否包含父默认True"""
    cls = type(obj)
    queryset = cls.objects.none()
    parent_queryset = cls.objects.filter(pk=obj.id)
    if has_parent:
        queryset = queryset | parent_queryset
    child_queryset = cls.objects.filter(parent=obj)
    while child_queryset:
        queryset = queryset | child_queryset
        child_queryset = cls.objects.filter(parent__in=child_queryset)
    return queryset
